package com.ebt.m.proposal_v2.adapter;

import android.content.Context;
import android.view.View;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.interfaces.OnRxClickListener;
import com.ebt.m.proposal_v2.utils.RxUtils;
import com.ebt.m.proposal_v2.widget.view.CompoundTextView;

import java.util.List;

import butterknife.BindView;

/**
 * 计划书的产品列表
 *
 * @author archer.qi
 *         Created on 2017/9/25.
 */
public class ProductInProposalAdapter extends EBTRecyclerAdapter<InsuranceEntity, ProductInProposalAdapter.ProductInProposalViewHolder> {

    public ProductInProposalAdapter(Context context, List<InsuranceEntity> data) {
        super(context, data);
    }

    @Override
    protected ProductInProposalViewHolder createViewHolder(View itemView) {
        return new ProductInProposalViewHolder(getContext(), itemView);
    }

    @Override
    protected int createConvertView() {
        return R.layout.item_product_in_proposal_window;
    }

    public static class ProductInProposalViewHolder extends EBTRecyclerAdapter.EBTRecyclerViewHolder<InsuranceEntity> {
        @BindView(R.id.product_label)
        CompoundTextView productView;

        public ProductInProposalViewHolder(Context context, View itemView) {
            super(context, itemView);
        }

        @Override
        protected void onBindView(View itemView, InsuranceEntity data, int position) {
            setProductLabel(data.isMain());
            setProductName(data.productName);
            bindListener(data);
        }

        private void setProductLabel(boolean isMain) {
            if (isMain) {
                productView.setLeftDrawable(ThemeHelper.getDrawable(getContext(), R.drawable.ic_label_main));
            } else {
                productView.setLeftDrawable(ThemeHelper.getDrawable(getContext(), R.drawable.ic_label_additional));
            }
        }

        private void setProductName(String productName) {
            productView.setText(productName == null ? "" : productName);
        }

        private void bindListener(InsuranceEntity data) {
            RxUtils.click(itemView, new RxClickListener(getContext(), data));
        }

        private static class RxClickListener implements OnRxClickListener {
            private Context mContext;
            private InsuranceEntity mData;

            public RxClickListener(Context context, InsuranceEntity data) {
                this.mContext = context;
                this.mData = data;
            }

            @Override
            public void onRxClick(View view) {
                ProposalUtils.startProductDetailNewFromProposalMaking(mContext, mData.productId);
            }
        }
    }
}
